import md5 from './ccdzhang-dokey/md5.js';
import dokey from './ccdzhang-dokey/dokey.js';
import {showModalLogin} from '@/util/mologin.js'; 
import {
	getToken,
	getLanguage
} from '@/util/tokeCookien.js'
const BASE_URL = 'http://47.98.234.161:9101/api'
//const BASE_URL = 'http://www.crowxx.xyz:8001/api'
//const BASE_URL = 'http://39.108.214.217:8085/api'//旧地址

export const myRequest = (options) => {
	var token = getToken().token
	var Authorization = getToken() ? token : ''
	var secretdate = new Date().getTime(); //时间戳
	var secreted = md5(dokey.getkey() + secretdate); //私钥加时间戳
	let headerlist = options.header;
	headerlist.lang_id = getLanguage() ? getLanguage() : 1; //切换语言  中文1 英文2
	
	headerlist.Authorization = Authorization
	headerlist.sign = secreted
	headerlist.timestamp = secretdate.toString()
	// Bearer eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1aWQiOiIzNzE5IiwiZGV2aWNlTm8iOiI3ODNERjNGNi0wMkZFLTQ0OUQtQkIwMC00MEZDMDFGRDdFNDkiLCJleHAiOjE1OTc0NzY3MDB9.shLul3S5awLJxV18QXBn2E9oQggK-D_pQGdgREtDYWk
	// headerlist.Authorization = "Bearer eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1aWQiOiIzNjkyIiwiZGV2aWNlTm8iOiIxMjMxMyIsImV4cCI6MTU5NTEzODY3MH0.PNwMuXyIV_9cfzi0lMfVvm9KfNPWVxr4suHgsyD0aiE"; //用户token
	// headerlist.sign = 'c813bd7b2734dd30e0a3ae06119bfcab'; //接口签名=MD5(私钥+毫秒级时间戳),当前私钥:zmkj
	// headerlist.timestamp = '1592726567687'; //时间戳
	/* uni.showLoading({
		mask:false
	}); */
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data,
			header: headerlist || {},
			success: (res) => {
				if(res.data.status==403){
					showModalLogin()
				}{
					resolve(res.data)
				}
				
				/* setTimeout(function () {
				    uni.hideLoading();
				}, 1000); */
			},
			fail: (err) => {
				uni.showToast({
					title: '获取失败',
					icon: "none"
				})
				reject(err)
			}
		})
	})
}
export const myUploadFile = (options) => {
	var token = getToken().token
	var Authorization = getToken() ? token : ''
	var secretdate = new Date().getTime(); //时间戳
	var secreted = md5(dokey.getkey() + secretdate); //私钥加时间戳
	let headerlist = options.header;
	headerlist.lang_id = getLanguage() ? getLanguage() : 1; //切换语言  中文1 英文2
	//headerlist.Authorization = Authorization;
	headerlist.Authorization = 'Bearer eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1aWQiOiIzNzE2IiwiZGV2aWNlTm8iOiI3ODNERjNGNi0wMkZFLTQ0OUQtQkIwMC00MEZDMDFGRDdFNDkiLCJleHAiOjE1OTgwNjI2Njh9.UU_MTkuO9auLpzjwiBzeMk84yL90TS_med7IdQGmzoU';
	headerlist.sign = secreted;
	headerlist.timestamp = secretdate.toString();
	return new Promise((resolve, reject) => {
		/* const uploadTask = */ uni.uploadFile({
			url: BASE_URL + options.url,
			files: options.files,
			filePath: options.filePath,
			name: options.name,
			formData: options.formData,
			header: headerlist || {},
			success: (res) => {
				console.log(res)
				if (res.statusCode == 200) {
					var data = JSON.parse(res.data)
					resolve(data)
				} else {
					resolve(res)
				}
			},
			fail: (err) => {
				console.log(err)
				uni.showToast({
					title: '获取失败',
					icon: "none"
				})
				reject(err)
			}
		})
		/* res.onProgressUpdate((upres) => {
			uni.showLoading({
				mask: true,
				title: '已上传' + upres.progress + '%'
			});
			console.log('上传进度' + upres.progress);
			console.log('已经上传的数据长度' + upres.totalBytesSent);
			console.log('预期需要上传的数据总长度' + upres.totalBytesExpectedToSend);

			// 测试条件，取消上传任务。
			if (upres.progress == 100) {
				setTimeout(function() {
					uni.hideLoading();
				}, 500);
				res.abort();
			}
		}); */
	})
}
